package top.wu2020.lazy.ws.chat.infrastructure.persistence;

import top.wu2020.lazy.ws.chat.infrastructure.entity.UserDO;
import top.wu2020.lazy.ws.chat.infrastructure.converter.UserConverter;
import top.wu2020.lazy.ws.chat.infrastructure.jpa.lazy.UserLazyJpaRepository;
import top.wu2020.lazy.ws.chat.infrastructure.mapper.UserMapper;
import top.wu2020.lazy.ws.chat.domain.model.user.UserRepository;
import org.springframework.stereotype.Repository;
import java.util.stream.Collectors;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;
import jakarta.annotation.Resource;
import top.wu2020.lazy.ws.chat.domain.model.user.User;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import java.util.List;
import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;
/**
 * describe user 
 *
 * @author Jia wei Wu
 * @date 2024/05/24 08:52 晚上
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence 
 **/
@Repository
public class UserRepositoryImpl  implements  UserRepository {

    @Resource
    LazyLambdaStream lazyLambdaStream;

    @Resource
    UserLazyJpaRepository userLazyJpaRepository;

    /**
     * describe 新增
     *
     * @param user 新增     
     * @return {@link Result<User>} 新增后领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> story(User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        lazyLambdaStream.upsert(userDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 批量新增
     *
     * @param userList 批量新增     
     * @return {@link Result<List<User>>} 新增后领域对象集合     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<User>> batchStory(List<User> userList) {
        List<UserDO> userDOList = userList.stream().map(UserConverter.INSTANCE::fromUser).collect(Collectors.toList());
        lazyLambdaStream.upsert(userDOList);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个
     *
     * @param user 查询单个     
     * @return {@link Result<User>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> findOne(User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        User userOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(userDO), User.class);
        return ResultFactory.successOf(userOne);
    }

    /**
     * describe 查询多个
     *
     * @param user 查询多个     
     * @return {@link Result<List<User>>} 领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<List<User>> findList(User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        List<User> userList = lazyLambdaStream.selectList(LazyWrappers.lambdaWrapperBean(userDO), User.class);
        return ResultFactory.successOf(userList);
    }

    /**
     * describe 分页查询多个
     *
     * @param size 当前页数
     * @param current 当前页
     * @param user 分页查询多个     
     * @return {@link Result<LazyPage<User>>} 分页领域对象     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<LazyPage<User>> findPage(int size,int current,User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        LazyPage<User> lazyPage = new LazyPage<>(current,size);
        LazyPage<User> userLazyPage = lazyLambdaStream.selectPage(LazyWrappers.lambdaWrapperBean(userDO),lazyPage, User.class);
        return ResultFactory.successOf(userLazyPage);
    }

    /**
     * describe 删除
     *
     * @param user 删除     
     * @return {@link Result<User>}      
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<User> remove(User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        //  lazyLambdaStream.delete(LazyWrappers.lambdaWrapperBean(userDO));
        return ResultFactory.successOf();
    }

    /**
     * describe 是否存在
     *
     * @param user 领域对象     
     * @return {@link Result<Boolean>} 是否存在 true 存在，false 不存在     
     
     * @author Jia wei Wu
     * @date 2024/05/24 08:52 晚上
     **/

    @Override
    public Result<Boolean> exists(User user) {
        UserDO userDO = UserConverter.INSTANCE.fromUser(user);
        Boolean exists=lazyLambdaStream.exists(LazyWrappers.lambdaWrapperBean(userDO));
        return ResultFactory.successOf(exists);
    }

    /**
     * 获取随机用户
     *
     * @return
     */
    @Override
    public Result<User> getRandomUser() {
        User user = userLazyJpaRepository.getRandomUser();
        return ResultFactory.successOf(user);
    }
}